﻿
Namespace myBookmarks.Web
    ''' <summary>
    ''' Render view to string  
    ''' Adapted from: http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
    ''' </summary>
    Public Class PagedList(Of T)
        Inherits List(Of T)
        Implements IPagedList(Of T)
        Public Sub New(ByVal source As IEnumerable(Of T), ByVal index As Integer, ByVal pageSize As Integer)
            Me.New(source, index, pageSize, Nothing)
        End Sub

        Public Sub New(ByVal source As IEnumerable(Of T), ByVal index As Integer, ByVal pageSize As Integer, ByVal totalCount As System.Nullable(Of Integer))
            Initialize(source.AsQueryable(), index, pageSize, totalCount)
        End Sub
#Region "IPagedList Members"

        Private _PageCount As Integer
        Public Property PageCount() As Integer Implements IPagedList(Of T).PageCount
            Get
                Return _PageCount
            End Get
            Private Set(ByVal value As Integer)
                _PageCount = value
            End Set
        End Property
        Private _TotalItemCount As Integer
        Public Property TotalItemCount() As Integer Implements IPagedList(Of T).TotalItemCount
            Get
                Return _TotalItemCount
            End Get
            Private Set(ByVal value As Integer)
                _TotalItemCount = value
            End Set
        End Property
        Private _PageIndex As Integer
        Public Property PageIndex() As Integer Implements IPagedList(Of T).PageIndex
            Get
                Return _PageIndex
            End Get
            Private Set(ByVal value As Integer)
                _PageIndex = value
            End Set
        End Property
        Public ReadOnly Property PageNumber() As Integer Implements IPagedList(Of T).PageNumber
            Get
                Return PageIndex + 1
            End Get
        End Property
        Private _PageSize As Integer
        Public Property PageSize() As Integer Implements IPagedList(Of T).PageSize
            Get
                Return _PageSize
            End Get
            Private Set(ByVal value As Integer)
                _PageSize = value
            End Set
        End Property
        Private _HasPreviousPage As Boolean
        Public Property HasPreviousPage() As Boolean Implements IPagedList(Of T).HasPreviousPage
            Get
                Return _HasPreviousPage
            End Get
            Private Set(ByVal value As Boolean)
                _HasPreviousPage = value
            End Set
        End Property
        Private _HasNextPage As Boolean
        Public Property HasNextPage() As Boolean Implements IPagedList(Of T).HasNextPage
            Get
                Return _HasNextPage
            End Get
            Private Set(ByVal value As Boolean)
                _HasNextPage = value
            End Set
        End Property
        Private _IsFirstPage As Boolean
        Public Property IsFirstPage() As Boolean Implements IPagedList(Of T).IsFirstPage
            Get
                Return _IsFirstPage
            End Get
            Private Set(ByVal value As Boolean)
                _IsFirstPage = value
            End Set
        End Property
        Private _IsLastPage As Boolean
        Public Property IsLastPage() As Boolean Implements IPagedList(Of T).IsLastPage
            Get
                Return _IsLastPage
            End Get
            Private Set(ByVal value As Boolean)
                _IsLastPage = value
            End Set
        End Property

#End Region

        Protected Sub Initialize(ByVal source As IQueryable(Of T), ByVal index As Integer, ByVal pageSize__1 As Integer, ByVal totalCount As System.Nullable(Of Integer))
            If index < 0 Then
                Throw New ArgumentOutOfRangeException("PageIndex cannot be below 0.")
            End If
            If pageSize__1 < 1 Then
                Throw New ArgumentOutOfRangeException("PageSize cannot be less than 1.")
            End If

            '### set source to blank list if source is null to prevent exceptions
            If source Is Nothing Then
                source = New List(Of T)().AsQueryable()
            End If

            '### set properties
            'If Not totalCount.HasValue Then
            '    TotalItemCount = source.Count()
            'End If
            If totalCount.HasValue Then
                TotalItemCount = totalCount
            End If
            PageSize = pageSize__1
            PageIndex = index
            If TotalItemCount > 0 Then
                PageCount = CInt(Math.Ceiling(TotalItemCount / CDbl(PageSize)))
            Else
                PageCount = 0
            End If
            HasPreviousPage = (PageIndex > 0)
            HasNextPage = (PageIndex < (PageCount - 1))
            IsFirstPage = (PageIndex <= 0)
            IsLastPage = (PageIndex >= (PageCount - 1))

            '### add items to internal list
            'If TotalItemCount > 0 Then
            '    AddRange(source.Skip((index) * pageSize__1).Take(pageSize__1).ToList())
            'End If
            If TotalItemCount > 0 Then
                AddRange(source.ToList())
            End If
        End Sub
    End Class
End Namespace
